﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MyCommon;
using DataAccessLayer;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

namespace QLBaoHanh.BaoCao
{
    public partial class frmReportViewer<DataType> : Form
    {
        private MyReportType _reportType;
        private IEnumerable<DataType> _dataSource;
        bool _isToPrint = false;
        private Dictionary<string, string> _reportParam;
        public Dictionary<string, string> ReportParam
        {
            get { return _reportParam; }
            set { _reportParam = value; }
        }
        public frmReportViewer(MyReportType inReportType, IEnumerable<DataType> inDataSource
            , bool inIsToPrint = false, Dictionary<string, string> inReportParam = null)
        {
            _reportType = inReportType;
            _dataSource = inDataSource;
            _isToPrint = inIsToPrint;
            _reportParam = inReportParam;
            if (_isToPrint)
            {
                PrinToPrinter();
                this.Close();
                this.Dispose();
            } 
            InitializeComponent();
            
        }

        private void PrinToPrinter()
        {
            if (_isToPrint)
            {
                ReportDocument rptDoc = new ReportDocument();
                switch (_reportType)
                {
                    case MyReportType.Report_PhieuTiepNhan:
                        //string path = Application.
                        rptDoc.Load("../../BaoCaoTemplate/rptPhieuTiepNhan.rpt");
                        break;
                    case MyReportType.Report_BaoHanhSuaChuaNgay:
                        rptDoc.Load("../../BaoCaoTemplate/rptPhien.rpt");
                        break;
                    case MyReportType.Report_TiepNhanBaoHanhSua:
                        this.Text = "Báo cáo danh tiếp nhận bảo hành, sửa chữa";
                        rptDoc.Load("../../BaoCaoTemplate/rptPhien.rpt");
                        break;
                    case MyReportType.Report_PhienKhongDungHan:
                        this.Text = "Báo cáo danh bảo hành, sửa chữa không đúng hạn";
                        rptDoc.Load("../../BaoCaoTemplate/rptPhienKhongDungHan.rpt");
                        break;

                    default:
                        break;
                }
                //set dataset to the report viewer.
                DataTable data = MyCommon.Utils.ToDataTable(_dataSource.ToList());
                rptDoc.SetDataSource(data);
                PassParameter(rptDoc);
                //Open the PrintDialog
                PrintDialog printDialog1 = new PrintDialog();
                System.Drawing.Printing.PrintDocument printDocument1 = new System.Drawing.Printing.PrintDocument();
                printDialog1.Document = printDocument1;
                DialogResult dr = printDialog1.ShowDialog();
                if (dr == DialogResult.OK)
                {
                    //Get the Copy times
                    int nCopy = printDocument1.PrinterSettings.Copies;
                    //Get the number of Start Page
                    int sPage = printDocument1.PrinterSettings.FromPage;
                    //Get the number of End Page
                    int ePage = printDocument1.PrinterSettings.ToPage;
                    //Get the printer name
                    string PrinterName = printDocument1.PrinterSettings.PrinterName;
                    //Set the printer name to print the report to. By default the sample
                    //report does not have a defult printer specified. This will tell the
                    //engine to use the specified printer to print the report. Print out 
                    //a test page (from Printer properties) to get the correct value.

                    rptDoc.PrintOptions.PrinterName = PrinterName;


                    //Start the printing process. Provide details of the print job
                    //using the arguments.
                    rptDoc.PrintToPrinter(nCopy, false, sPage, ePage);

                    //Let the user know that the print job is completed
                    MessageBox.Show("In thành công!");
                }
            }
        }

        private void ReportViewer_Load(object sender, EventArgs e)
        {
            ReportDocument rptDoc = new ReportDocument();
            
            switch (_reportType)
            {
                case MyReportType.Report_PhieuTiepNhan:
                    //string path = Application.
                    this.Text = "Phiếu tiếp nhận";
                    rptDoc.Load("../../BaoCaoTemplate/rptPhieuTiepNhan.rpt");
                    break;
                case MyReportType.Report_BaoHanhSuaChuaNgay:
                    this.Text = "Báo cáo danh sách bảo hành, sửa chữa ngay";
                    rptDoc.Load("../../BaoCaoTemplate/rptPhien.rpt");
                    break;
                case MyReportType.Report_TiepNhanBaoHanhSua:
                    this.Text = "Báo cáo danh tiếp nhận bảo hành, sửa chữa";
                    rptDoc.Load("../../BaoCaoTemplate/rptPhien.rpt");
                    break;
                case MyReportType.Report_PhienKhongDungHan:
                    this.Text = "Báo cáo danh bảo hành, sửa chữa không đúng hạn";
                    rptDoc.Load("../../BaoCaoTemplate/rptPhienKhongDungHan.rpt");
                    break;
                default:
                    break;
            }
            //PassParameter(rptDoc);
            //set dataset to the report viewer.

            DataTable data = MyCommon.Utils.ToDataTable(_dataSource.ToList());
            rptDoc.SetDataSource(data);
            PassParameter(rptDoc);
            this.crystalReportViewer1.ReportSource = rptDoc;
        }

        private void PassParameter(ReportDocument inRptDoc)
        {
            if (_reportParam == null) return;
            foreach (KeyValuePair<string, string> pair in _reportParam)
            {
                string parameterName = pair.Key;
                string parameterValue = pair.Value;
                inRptDoc.SetParameterValue(parameterName, parameterValue);
            }
        }
    }
}
